/**
 * 
 */
package net.danielpaz.puc.poo.frigorifico.model;

import java.sql.*;

/**
 * @author Junior
 * 
 */
public class UsuarioDAO {
	public BD bd;
	public Usuario usuario;
	private PreparedStatement statement;
	private ResultSet resultSet;
	private String men, sql;
	public static final byte INCLUSAO = 1;
	public static final byte ALTERACAO = 2;
	public static final byte EXCLUSAO = 3;

	public UsuarioDAO() {
		bd = new BD();
		usuario = new Usuario();
	}

    	/* 
         * Localiza o Usuario atravez da variavel usuario
	 * @param
	 * @return boolean se encontrou ou nao
	 * 
	 */
	public boolean localizar() {
		sql = "select * from usuario where id = ?";
		try {
			statement = bd.connection.prepareStatement(sql);
			statement.setInt(1, usuario.getId());
			resultSet = statement.executeQuery();
			while (resultSet.next()) {
				usuario.setId(resultSet.getInt(1));
				usuario.setNome(resultSet.getString(2));
				usuario.setCpf(resultSet.getString(3));
				usuario.setRg(resultSet.getString(4));
				usuario.setEndereco(resultSet.getString(5));
				usuario.setTelefone(resultSet.getString(6));
				usuario.setUsuario(resultSet.getString(7));
				usuario.setSenha(resultSet.getString(8));
				usuario.setStatus(resultSet.getInt(9));
			}
			return true;
		} catch (SQLException erro) {
			erro.printStackTrace();
			return false;
		}
	}

	/* 
         * Metodo que realizaca a operacao (insert, update ou delete)
	 * @param int operacao
	 * @return String se conseguiu ou nao realizar a operacao solicitada
	 * 
	 */
	public String atualizar(int operacao) {
		men = "Operacao realizada com sucesso!";
		try {
			if (operacao == INCLUSAO) {
				sql = "insert into usuario values (?,?,?,?,?,?,?,?,?)";
				statement = bd.connection.prepareStatement(sql);
				statement.setInt(1, usuario.getId());
				statement.setString(2, usuario.getNome());
				statement.setString(3, usuario.getCpf());
				statement.setString(4, usuario.getRg());
				statement.setString(5, usuario.getEndereco());
				statement.setString(6, usuario.getTelefone());
				statement.setString(7, usuario.getUsuario());
				statement.setString(8, usuario.getSenha());
				statement.setInt(9, usuario.getStatus());
			} else if (operacao == ALTERACAO) {
				sql = "update usuario set nome = ?, cpf = ?, rg = ?, "
						+ "endereco = ?,telefone = ?,usuario = ?,senha = ?, status = ? where id = ?";
				statement = bd.connection.prepareStatement(sql);
				statement.setString(1, usuario.getNome());
				statement.setString(2, usuario.getCpf());
				statement.setString(3, usuario.getRg());
				statement.setString(4, usuario.getEndereco());
				statement.setString(5, usuario.getTelefone());
				statement.setString(6, usuario.getUsuario());
				statement.setString(7, usuario.getSenha());
				statement.setInt(8, usuario.getStatus());
				statement.setInt(9, usuario.getId());
			} else if (operacao == EXCLUSAO) {
				sql = "delete from usuario where id = ?";
				statement = bd.connection.prepareStatement(sql);
				statement.setInt(1, usuario.getId());
			}
			if (statement.executeUpdate() == 0)
				men = "Falha na operacao!";
		} catch (SQLException erro) {
			men = "Falha na operacao " + erro.toString();
		}
		return men;
	}

	/* 
         * Metodo para realizar o login
	 * @param
	 * @return boolean se pode ou nao fazer o login no sistema
	 * 
	 */
	public boolean login() {
		sql = "select count(*) from usuario where usuario = ?,senha = ?";

		try {
			statement = bd.connection.prepareStatement(sql);
			statement.setString(1, usuario.getUsuario());
			statement.setString(2, usuario.getSenha());
			resultSet = statement.executeQuery();
			
			if(Integer.parseInt(resultSet.getString(0)) > 0) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException erro) {
			erro.printStackTrace();
			}
		return false;
		
		
		
	}
}
